package psn.kiko.controller.file;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import psn.kiko.annotation.api.StableID;
import psn.kiko.constant.MessageConstant;
import psn.kiko.constant.MimeTypePrefixConstant;
import psn.kiko.constant.ResponseCodeConstant;
import psn.kiko.pojo.PageResult;
import psn.kiko.pojo.Result;
import psn.kiko.pojo.dto.FileDTO;
import psn.kiko.pojo.vo.ServerPhysicFileVO;
import psn.kiko.service.business.system.FileService;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;

/**
 * 文件控制器
 */
@RestController
@RequestMapping("/admin/file")
@Api(tags={"文件管理"})
public class FileController{
	@Autowired
	FileService fileService;
	
	@DeleteMapping
	@ApiOperation("删除所有已上传文件")
	@StableID(601)
	Result deleteAll() throws FileNotFoundException{
		return Result.success(MessageConstant.FILE_DELETION_SUCCESS,fileService.deleteAll());
	}
	
	@GetMapping
	@ApiOperation("根据文件绝对路径下载文件")
	@StableID(603)
	public void download(
			@ApiParam(allowMultiple=true, value="文件绝对路径")
			@RequestParam("absolutePath")
					String absolutePath,
			HttpServletResponse response) throws FileNotFoundException{
		fileService.download(absolutePath,response);
	}
	
	@GetMapping("/audio/{pageNum}/{pageSize}")
	@ApiOperation("分页获取audio文件信息")
	@StableID(604)
	public Result<?> pagingGetAudios(
			@PathVariable("pageNum") int pageNum,
			@PathVariable("pageSize") int pageSize
	){
		PageResult<Object> pageResult=fileService.pagingFilesByMinePrefix(MimeTypePrefixConstant.AUDIO,pageNum,pageSize);
		return Result.success(pageResult);
	}
	
	@GetMapping("/video/{pageNum}/{pageSize}")
	@ApiOperation("分页获取video文件信息")
	@StableID(605)
	public Result<?> pagingGetVideos(
			@PathVariable("pageNum") int pageNum,
			@PathVariable("pageSize") int pageSize
	){
		PageResult<Object> pageResult=fileService.pagingFilesByMinePrefix(MimeTypePrefixConstant.VIDEO,pageNum,pageSize);
		return Result.success(pageResult);
	}
	
	@GetMapping("/image/{pageNum}/{pageSize}")
	@ApiOperation("分页获取image文件信息")
	@StableID(606)
	public Result<?> pagingGetImages(
			@PathVariable("pageNum") int pageNum,
			@PathVariable("pageSize") int pageSize
	){
		PageResult<Object> pageResult=fileService.pagingFilesByMinePrefix(MimeTypePrefixConstant.IMAGE,pageNum,pageSize);
		return Result.success(pageResult);
	}
	
	@GetMapping("/childrenOfDir")
	@ApiOperation("获取指定目录的子文件信息集合")
	@StableID(608)
	public Result<?> childrenOfDir(
			@ApiParam(value="目录的绝对路径", required=true)
			@RequestParam("dirAbsolutePath")
					String dirAbsolutePath
	) throws FileNotFoundException{
		List<ServerPhysicFileVO> serverPhysicFileVOS=fileService.childrenOfDirLTClasspath(dirAbsolutePath);
		return Result.success(serverPhysicFileVOS);
	}
	
	@GetMapping("/returnParentDirOfCurDir")
	@ApiOperation("返回指定文件所在目录的父目录的子文件信息集合")
	@StableID(609)
	public Result<?> returnParentDirOfCurDir(
			@ApiParam(value="指定的文件", required=true)
			@RequestParam("specificFile")
					String specificFile
	) throws FileNotFoundException{
		List<ServerPhysicFileVO> fileVOS=fileService.returnParentDirOfCurDirLTClasspath(specificFile);
		return Result.success(fileVOS);
	}
	
	// 可访问的资源根路径为classpath对应的文件目录
	@GetMapping("/visibilityOfResourceRoot")
	@ApiOperation("可访问的资源根路径")
	@StableID(610)
	public Result<Object> getResouceRootDirectory() throws FileNotFoundException{
		String path=ResourceUtils.getURL("classpath:").getPath();
		String rootPath=new File(path).getAbsolutePath();
		Result<Object> result=Result.builder()
				.code(ResponseCodeConstant.BUSINESS_OPERATION_SUCCESS)
				.msg(MessageConstant.BUSINESS_OPERATION_SUCCESS)
				.data(rootPath).build();
		return result;
	}
	
	@PostMapping("/image")
	@ApiOperation("上传图片")
	@StableID(611)
	public Result uploadImage(
			@ApiParam(value="需要上传的图片集合", allowMultiple=true) @RequestParam("file") MultipartFile[] files){
		List<String> list = fileService.batchAddFile(files);
		return Result.success(MessageConstant.FILE_UPLOAD_SUCCESS,list);
	}
	
	@PostMapping("/audio")
	@ApiOperation("上传音频")
	@StableID(612)
	public Result uploadAudio(
			@ApiParam(value="需要上传的音频集合", allowMultiple=true) @RequestParam("file") MultipartFile[] files){
		List<String> list = fileService.batchAddFile(files);
		return Result.success(MessageConstant.FILE_UPLOAD_SUCCESS,list);
	}
	
	@PostMapping("/video")
	@ApiOperation("上传视频")
	@StableID(613)
	public Result uploadVideo(
			@ApiParam(value="需要上传的视频集合", allowMultiple=true) @RequestParam("file") MultipartFile[] files){
		List<String> list = fileService.batchAddFile(files);
		return Result.success(MessageConstant.FILE_UPLOAD_SUCCESS,list);
	}
	
	@DeleteMapping("/audio/batch")
	@ApiOperation("批量删除音频文件及文件信息")
	@StableID(614)
	public Result deleteAudios(@RequestBody FileDTO[] fileDTOList){
		Integer deleteNum=fileService.deleteByDtos(Arrays.asList(fileDTOList));
		return Result.success(deleteNum);
	}
	
	@DeleteMapping("/image/batch")
	@ApiOperation("批量删除图片文件及文件信息")
	@StableID(615)
	public Result deleteImages(@RequestBody FileDTO[] fileDTOList){
		Integer deleteNum=fileService.deleteByDtos(Arrays.asList(fileDTOList));
		return Result.success(deleteNum);
	}
	
	@DeleteMapping("/video/batch")
	@ApiOperation("批量删除视频文件及文件信息")
	@StableID(616)
	public Result deleteVideos(@RequestBody FileDTO[] fileDTOList){
		Integer deleteNum=fileService.deleteByDtos(Arrays.asList(fileDTOList));
		return Result.success(deleteNum);
	}
}
